﻿Imports System.Data.SqlClient
Public Class CommonConUtil

    Private Shared _cn As SqlConnection = Nothing

    Private Shared _连接字符串 As String = ""
    Public Shared Property 连接字符串() As String
        Get
            Return _连接字符串
        End Get
        Set(ByVal value As String)
            _连接字符串 = value
        End Set
    End Property

    Public Shared Function GetCon() As SqlConnection

        Try
            If _cn Is Nothing Then
                _cn = New SqlConnection(连接字符串)
                _cn.Open()
            End If

            If _cn.State = ConnectionState.Closed Then
                _cn.Open()
            End If

        Catch ex As Exception

            Debug.WriteLine(ex.ToString)

        End Try

        Return _cn

    End Function

    Public Shared Function ExecuteSql(ByVal Sql As String) As Boolean

        Try

            Using cmd As New SqlCommand
                cmd.Connection = GetCon()
                cmd.CommandText = Sql
                cmd.ExecuteNonQuery()
            End Using

        Catch ex As Exception

            Debug.WriteLine(ex.ToString)

        End Try

    End Function

    Public Shared Function GetTable(ByVal sql As String) As DataTable

        Dim dt As New DataTable

        Try

            Using sqa As New SqlDataAdapter(sql, GetCon)
                sqa.Fill(dt)
            End Using

        Catch ex As Exception

            Debug.WriteLine(ex.ToString)

        End Try


        Return dt

    End Function

    Public Shared Function CloseCon() As Boolean

        Try
            _cn.Close()
        Catch ex As Exception

            Debug.WriteLine(ex.ToString)

        End Try

    End Function

End Class
